class Solution {
    public int[] productExceptSelf(int[] nums) {
        int length = nums.length;
        int[] pre = new int[length];
        pre[0] = nums[0];
        int[] last = new int[length];
        last[length-1] = nums[length-1];
        int[] cur = new int[length];
        for(int i = 1;i < length;i++) {
            pre[i] = pre[i-1]*nums[i];
        }
        for(int i = length-2;i >= 0;i--) {
            last[i] = last[i+1]*nums[i];
        }
        for(int i = 0;i < length;i++) {
            if(i == 0) {
                cur[0] = last[1];
            }else if(i == length-1) {
                cur[i] = pre[length-2];
            } else {
                cur[i] = pre[i-1] * last[i+1];
            }
        }
        return cur;
    }
}